#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {

	FILE *fp_in = NULL, *fp_out = NULL, *fp_sample;

	//fp_in = fopen("E:\\Projects\\consensus.rar", "rb");
	//fp_sample = fopen("E:\\Projects\\csgPack\\_Samples.rar", "rb");
	//fp_out = fopen("E:\\Projects\\csgPack\\555.rar", "wb");

	if(argc < 4)
		return -1;
	fp_in = fopen(argv[1], "rb");
	fp_sample = fopen(argv[2], "rb");
	fp_out = fopen(argv[3], "wb");

	if(fp_sample == NULL)
		return 0;
	fseek(fp_sample, 0, SEEK_END);
	long length = ftell(fp_sample);
	char *buff = NULL;
	buff = new char[length];
	rewind(fp_sample);
	fread(buff, sizeof(char), length, fp_sample);
	fwrite(buff, sizeof(char), length, fp_out);
	delete []buff;

	if((fp_in != NULL) && (fp_out != NULL)) {
		fseek(fp_in, 0, SEEK_END);
		fpos_t end_pos;
		fgetpos(fp_in, &end_pos);
		rewind(fp_in);

		const fpos_t buff_len = 536870912;	// 512M
		char *buff = new char[buff_len];
		fpos_t cur_pos, atual_len;
		cur_pos = 0;
		while(cur_pos < end_pos)
		{
			atual_len = (buff_len<(end_pos-cur_pos))?buff_len:(end_pos-cur_pos);
			fread(buff, sizeof(char), atual_len, fp_in);
			for(fpos_t i=0; i<atual_len; i++)
				buff[i] = ~buff[i];
			fwrite(buff, sizeof(char), atual_len, fp_out);
			cur_pos = cur_pos + atual_len;
		}
		delete []buff;

		fclose(fp_in);
		fclose(fp_out);
	}

	system("pause");
	return 0;
}
